package com.spps;

import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;

public class TemplateParser {
    String content = "";

    String loopStart = "\\{#each#\\}";

    String loopEnd = "\\{#endeach#\\}";

    String itemPattern = "\\{#item\\.(.*?)#\\}";

    TemplateParser(String content) {
        this.content = content;
    }

    public void parseLoop(ArrayList<HashMap<String, String>> dataSource) {
        // 获取循环模板
        String loopTpl = "";
        Pattern pattern = Pattern.compile(loopStart + "([\\s\\S]+)" + loopEnd);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            loopTpl = matcher.group(1).trim();
        }

        // 开始编译循环模板
        String result = "";
        for (int i = 0; i < dataSource.size(); i++) {
            HashMap<String, String> element = dataSource.get(i);
            result += loopTpl;
            for (HashMap.Entry<String, String> entry : element.entrySet()) {
                result = result.replaceAll("\\{#item\\." + entry.getKey() + "#\\}", entry.getValue());
            }
        }

        content = content.replaceAll(loopStart + "[\\s\\S]+" + loopEnd, result);
    }

    public void parseVariable(HashMap<String, String> dataSource) {
        for (HashMap.Entry<String, String> entry : dataSource.entrySet()) {
            // 替换带有修饰符的
            Pattern pattern1 = Pattern.compile("%" + entry.getKey() + "\\|capitalizes%");
            Matcher matcher1 = pattern1.matcher(content);
            content = matcher1.replaceAll(StringUtils.capitalizes(entry.getValue()));

            // 替换没有修饰符的
            Pattern pattern2 = Pattern.compile("%" + entry.getKey() + "%");
            Matcher matcher2 = pattern2.matcher(content);
            content = matcher2.replaceAll(entry.getValue());
        }
    }

    public void parseEquals(HashMap<String, String> dataSource) {
        Pattern ifPattern = Pattern.compile("\\{#equals\\((.*?),(.*?)\\)#\\}");
        Matcher ifMatcher = ifPattern.matcher(content);
        if (ifMatcher.find()) {
            String param1 = ifMatcher.group(1);
            String param2 = ifMatcher.group(2);
            if(dataSource.get(param1).equals(dataSource.get(param2))) {
                content = content.replaceAll("\\{#equals.*?#\\}(.*?)\\{#endequals#\\}", "$1");        
            } else {
                content = content.replaceAll("\\{#equals.*?#\\}.*?\\{#endequals#\\}", "");
            }
        }

        Pattern notEqualsPattern = Pattern.compile("\\{#notEquals\\((.*?),(.*?)\\)#\\}");
        Matcher notEqualsMatcher = notEqualsPattern.matcher(content);
        if (notEqualsMatcher.find()) {
            String param1 = notEqualsMatcher.group(1);
            String param2 = notEqualsMatcher.group(2);
            if(!dataSource.get(param1).equals(dataSource.get(param2))) {
                content = content.replaceAll("\\{#notEquals.*?#\\}(.*?)\\{#endNotEquals#\\}", "$1");        
            } else {
                content = content.replaceAll("\\{#notEquals.*?#\\}.*?\\{#endNotEquals#\\}", "");
            }
        }
    }

    String getItemKey(String content) {
        Pattern pattern = Pattern.compile(itemPattern);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    String compile() {
        return content;
    }
}
